package com.common.help.test02;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketServer2 {

	public static void main(String[] args) throws Exception {
		
		long start = System.currentTimeMillis();
		int port = 12345;
		
		ServerSocket server = new ServerSocket(port);
		System.out.println("服务端打开 将一直等待连接的到来");
		Socket socket = server.accept();
		InputStream is = socket.getInputStream();
		byte[] b = new byte[2048];
		int len=0;
		StringBuffer sb = new StringBuffer();
		while((len=is.read(b))!=-1){
			sb.append(new String(b,0,len,"UTF-8"));
		}
		System.out.println("get message from client "+sb);
		
		//发消息回馈给客户端
		OutputStream os = socket.getOutputStream();
		os.write("我们收到你们的消息了 再见".getBytes());
		os.close();
		is.close();
		socket.close();
		server.close();
		long end = System.currentTimeMillis();
		System.out.println("你丫的 我等待这就你在进来"+((end-start)/1000D)+"s");
	}
}
